package cn.isjinhao.se.sql.grammar.statement;

import cn.isjinhao.se.sql.function.evaluator.FunctionEvaluator;
import cn.isjinhao.se.sql.grammar.CurdStatement;
import cn.isjinhao.se.sql.grammar.GrammarErrorReporterDelegate;
import cn.isjinhao.se.sql.grammar.statement.create.InsertParser;
import cn.isjinhao.se.sql.grammar.statement.delete.DeleteParser;
import cn.isjinhao.se.sql.grammar.statement.retrieve.SelectParser;
import cn.isjinhao.se.sql.grammar.statement.update.UpdateParser;
import cn.isjinhao.se.sql.lexical.scan.TokenSequence;
import cn.isjinhao.se.sql.lexical.token.TokenType;

/**
 * @Author ISJINHAO
 * @Date 2021/4/10 9:52
 */
public class CurdParser extends StatementParser {

    // 这里可以对 Parser做一层缓存，避免每次都new对象引起Full GC。

    public CurdParser(TokenSequence tokenSequence, FunctionEvaluator functionEvaluator) {
        super(tokenSequence, functionEvaluator);
    }

    @Override
    public CurdStatement parse() {
        if(tokenSequence.equalThenAdvance(TokenType.SELECT)) {
            return new SelectParser(tokenSequence, getFunctionEvaluator()).parse();
        } else if(tokenSequence.equalThenAdvance(TokenType.INSERT)) {
            return new InsertParser(tokenSequence, getFunctionEvaluator()).parse();
        } else if(tokenSequence.equalThenAdvance(TokenType.DELETE)) {
            return new DeleteParser(tokenSequence, getFunctionEvaluator()).parse();
        } else if(tokenSequence.equalThenAdvance(TokenType.UPDATE)) {
            return new UpdateParser(tokenSequence, getFunctionEvaluator()).parse();
        }
        error(GrammarErrorReporterDelegate.CURD_UNKNOW_SQL_SYNTAX_PARSE);
        return null;
    }

    @Override
    protected void doFunctionStaticCheck(Function function) {
        // nop
    }

}
